package com.example.common.controller;

import com.example.common.service.DictService;
import com.example.common.util.core.controller.BaseController;
import com.example.common.util.core.resultbody.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 数据字典Controller
 *
 * @author ruoyi
 * @date 2021-07-10
 */
@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/admin/common/dict")
public class DictController extends BaseController
{
    @Autowired
    private DictService dictService;


    /**
     * 导出数据字典列表
     */
    @ApiOperation("导出数据字典列表")
    @GetMapping("/export")
    public void exportDict(HttpServletResponse response) throws IOException {
        byte[] data = dictService.exportDict(response);
        genZip(response,data);
    }


    /**
     * 导入数据字典列表
     */
    @ApiOperation("导入数据字典列表")
    @GetMapping("/import")
    public AjaxResult importDict(MultipartFile file)
    {
        return AjaxResult.success(dictService.importDictData(file));
    }


    /**
     * 根据dictCode获取下级节点
     */
    @ApiOperation("根据dictCode获取下级节点")
    @GetMapping("getDict/{dictCode}")
    public AjaxResult getDictByDictCode(@PathVariable("dictCode") String dictCode)
    {
        return AjaxResult.success(dictService.getDictByDictCode(dictCode));
    }


    /**
     * 根据数据id查询子数据列表
     */
    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping("getChildData/{id}")
    public AjaxResult getChildData(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dictService.getChildDataById(id));
    }

    /**
     * 根据dictcode和value查询
     */
    @ApiOperation("根据dictcode和value查询")
    @GetMapping("getName/{dictCode}/{value}")
    public AjaxResult getName(@PathVariable String dictCode,
                              @PathVariable String value)
    {
        return AjaxResult.success(dictService.getDictName(dictCode,value));
    }

    /**
     * 根据value查询
     */
    @ApiOperation("根据value查询")
    @GetMapping("getName/{value}")
    public AjaxResult getName(@PathVariable String value)
    {
        return AjaxResult.success(dictService.getDictName(null,value));
    }


    /**
     * 下载zip文件
     * @param response
     * @param data
     * @throws IOException
     */
    private void genZip(HttpServletResponse response, byte[] data) throws IOException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"dict.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

}
